package com.starssea.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service
public class ReadConfig {

    // 注入环境对象
    @Autowired
    private Environment env;

    public void printEnv() {
        // 获取某个key的值
        String name = env.getProperty("app.name");

        // 判断某个key是否存在
        if (env.containsProperty("app.owner")) {
            System.out.println("app.owner存在");
        }

        // 读取key的值，转为期望的类型，同时提供默认值
        Integer port = env.getProperty("app.port", Integer.class, 8080);

        String str = String.format("读取key的值，name=%s，port=%d", name, port);
        System.out.println(str);
    }

}
